<?php
/**
 * 用户列表
 * author itholiday@126.com
 */
global $_W, $_GPC;
load()->func('tpl');
$html_title = $_W['current_module']['title'];
$op = !empty($_GPC['op']) ? $_GPC['op'] : 'list';
//页面显示条数
$pagesize = 15;
$page = max(1, $_GPC['page']);
$start = ($page - 1) * $pagesize;

$where = 'A.id>0 ';

$keyword = isset($_GPC['keyword']) && !empty($_GPC['keyword']) ? input('keyword') : '';
$start_time = isset($_GPC['start_time']) && !empty($_GPC['start_time']) ? strtotime($_GPC['start_time']) : '';
$end_time = isset($_GPC['end_time']) && !empty($_GPC['end_time']) ? strtotime($_GPC['end_time']) : '';


if (!empty($keyword)){
    $where .= " and (A.id='$keyword' or A.mobile='$keyword'  or A.truename like '%$keyword%') ";
}

if (!empty($start_time) && !empty($end_time)) {
    $where .= " and A.create_time>=$start_time and A.create_time<=$end_time ";
} else {
    if (!empty($start_time)) {
        $where .= " and A.create_time>=$start_time ";
    }
    if (!empty($end_time)) {
        $where .= " and A.create_Time<=$end_time ";
    }
}
if($op == 'add'){
    if (is_ajax()){
        error_reporting(E_ERROR);
        ini_set('display_errors',0);
        $data['pid'] = intval($_GPC['pid']);
        $data['user_pass'] = cmf_password($_GPC['user_pass']);
        $data['mobile'] = intval($_GPC['mobile']);
        $data['sex'] = intval($_GPC['sex']);
        $data['alipay_name'] = trim($_GPC['alipay_name']);
        $data['alipay_account'] = trim($_GPC['alipay_account']);
        $data['bank_card'] = intval($_GPC['bank_card']);
        $data['truename'] = trim($_GPC['truename']);


        if (!empty($data['mobile'])){
            $mobile = $data['mobile'];
            $user = pdo_get('yq_haircircle_users',array('mobile' => $mobile));
            if (!empty($user)){
                ajaxError('添加失败,已存在此会员');
            }
        }
        $res = pdo_insert('yq_haircircle_users',$data);
        if (!empty($res)){
            ajaxSuccess('添加成功');
        }
        $user_id = pdo_insertid();
        $url = 'http://' . $_SERVER['HTTP_HOST'] . '/app/' . $this->createMobileUrl('public', array('op' => 'register', 'pid' => $user_id));
        $tuiguang_setting = cmf_get_option('tuiguang_setting');
        if(isset($tuiguang_setting['thumb1']) && !empty($tuiguang_setting['thumb1'])){
            $bg = '../attachment/' . cmf_asset_relative_url($tuiguang_setting['thumb1']);
        }else{
            $bg = '../addons/yq_haircircle/qrcode-background.png';
        }
        $qrcode = cmf_get_qrcode($url,251,$bg);
        $updata['user_login'] = $user_id;
        $updata['qrcode'] = $qrcode ? $qrcode : '';
        pdo_update('yq_haircircle_users',$updata,array('id'=>$user_id));
        ajaxError('添加失败');
    }

    include $this->template("web/member_add");
}

if($op == 'edit'){
    if(is_ajax()){
        $id = input('id');
        $data['alipay_name'] = input('alipay_name');
        $data['alipay_account'] = input('alipay_account');
        $data['bank_card'] = input('bank_card');
        $data['bank_name'] = input('bank_name');
        $data['bank_truename'] = input('bank_truename');
        $data['bank_branch'] = input('bank_branch');
//        $data['user_money'] = input('user_money');

        $user = pdo_get('yq_haircircle_users',array('id'=>$id));
        if($user['user_money'] != $data['user_money']){
            pdo_begin();
            $change =  $data['user_money']-$user['user_money'] ;
            //用户余额变更日志
            $log['oid'] = pdo_insertid();
            $log['user_id'] = $user_id;
            $log['type'] = $change>0 ? 4 : -2;
            $log['change'] = $change;
            $log['balance'] = $data['user_money'];
            $log['mobile'] = $user['mobile'];
            $log['remark'] = $change>0 ? '系统加钱' : '系统扣钱';
            $log['create_time'] = time();
            $res = pdo_insert('yq_haircircle_user_money_log', $log);
            if (empty($res)) {
                pdo_rollback();
                ajaxError('修改失败(写入用户余额变更日志出现异常),请稍后重试');
            }
            //更改信息
            $res = pdo_update('yq_haircircle_users',$data,array('id'=>$id));
            if(empty($res)){
                pdo_rollback();
                ajaxError('修改失败');
            }
            pdo_commit();
        }else{
            $res = pdo_update('yq_haircircle_users',$data,array('id'=>$id));
            if(empty($res)){
                ajaxError('修改失败');
            }
        }
        ajaxSuccess('修改成功');
    }
    $id = input('id');
    $user = pdo_get('yq_haircircle_users',array('id'=>$id));
    include $this->template("web/member_edit");
}

if($op == 'resetpassword'){
    if(is_ajax()){
        $id = input('id');
        $password = input('password');
        $data['user_pass'] = empty($password) ? '' : cmf_password($password);

        $res = pdo_update('yq_haircircle_users',$data,array('id'=>$id));
        if(empty($res)){
            ajaxError('修改失败');
        }
        ajaxSuccess('修改成功');
    }
    $id = input('id');
    $user = pdo_get('yq_haircircle_users',array('id'=>$id));
    include $this->template("web/member_resetpassword");
}

if ($op == 'del') {
    if (isset($_GPC['ids'])) {
        $ids = implode(',', $_GPC['ids']);
        $sql = "update ims_yq_haircircle_users set delete_time=" . time() . " where id in ($ids)";
        $res = pdo_query($sql);
        if (!empty($res)) {
            ajaxSuccess('删除成功');
        }
        ajaxError('删除失败');
    }
    if (isset($_GPC['id'])) {
        $id = $_GPC['id'];
        $res = pdo_update('yq_haircircle_users', ['delete_time' => time()], array('id' => $id));
        if (!empty($res)) {
            ajaxSuccess('删除成功');
        }
        ajaxError('删除失败');
    }
}

if ($op == 'ting' && isset($_GPC['yes'])) {
    if (isset($_GPC['id'])) {
        $id = $_GPC['id'];
        $yes = $_GPC['yes'] ? 1 : 0;
        $msg = $yes ? '启用' : '禁用';
        $res = pdo_update('yq_haircircle_users', ['user_status' => $yes], ['id' => $id]);
        if (!empty($res)) {
            ajaxSuccess($msg . '成功');
        }
        ajaxError($msg . '失败');
    }
}

if ($op == 'list' || $op == '') {
    $where .= " and A.delete_time=0 ";
    $total = pdo_fetchcolumn('SELECT COUNT(*) AS `number` FROM ' . tablename('yq_haircircle_users') . ' A WHERE ' . $where);
    //生成分页HTML
    $pager = getPageHTML($total, $page, $pagesize);
    $sql = "select A.*,B.mobile pmobile from ims_yq_haircircle_users A left join ims_yq_haircircle_users B on A.pid=B.id where $where and A.delete_time=0 order by A.id desc limit $start,$pagesize";
    $list = pdo_fetchall($sql);
    include $this->template('web/member_list');
    exit;
}